package com.zhengbo.flink.demo.base.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * GpsUtil.java
 *
 * @author chenchuanyin
 * @version 0.0.1
 * @date 2018/9/12
 */
public class GpsUtil {

    public static final String LON = "lon";
    public static final String LAT = "lat";
    public static final String DATA_TIME = "dataTime";
    public static final String DRIVER_NO = "driverNo";
    public static final String CITY_CODE = "cityCode";

    public static void sortDistinct(List<Map<String,String>> points) {
        points.sort((last, next) -> {
            String time1 = last.get(DATA_TIME);
            String time2 = next.get(DATA_TIME);
            if (StringUtils.isNotBlank(time1)&&StringUtils.isNotBlank(time2)){
                return (int)(Long.valueOf(time1)-Long.valueOf(time2));
            }
            return 0;
        });
        Set<String> timeStampSet = new HashSet<>();
        for (int i=0;i<points.size();i++) {
            if (!timeStampSet.add(points.get(i).get(DATA_TIME))){
                points.remove(i);
                i--;
            }
        }
    }


    public static Map<String, String> initDriverLocationMap(Map<String, Object> message) {
        Map<String, String> driverLocationMap = new HashMap<>();
        String content = String.valueOf(message.get("content"));
        String driverNoStr;
        String lng;
        String lat;
        String cityCode;
//        short cmd = Short.valueOf(message.get("cmd").toString());
//        if (cmd == StormConfig.CMD_UPDATE_LOCATION) {
        //414111:120.217137,30.206442,25.130510:0571:EE6924672AB8EC1DE228CD36C87D5831
        //司机id：经度,维度,方向,**：城市
        String[] split = content.split(":");
        driverNoStr=split[0];
        String[] split1 = split[1].split(",");
        lng=split1[0];
        lat=split1[1];
        cityCode=split[2];
        driverLocationMap.put(DATA_TIME, String.valueOf(message.get("timestamp")));
        driverLocationMap.put(LON, lng);
        driverLocationMap.put(LAT, lat);
        driverLocationMap.put(DRIVER_NO, driverNoStr);
        driverLocationMap.put(CITY_CODE,cityCode);
        return driverLocationMap;
    }

    /**
     * 计算速度(m/s)
     */
    public static Map getGpsMap(NavigableMap<byte[], byte[]> familyMap) {
        Map<String, String> map = new HashMap<>();
        try {
            byte[] lastBytes = familyMap.values().iterator().next();
            String lastString = new String(lastBytes);
            JSONObject object = JSON.parseObject(lastString);

            if (object.containsKey(DATA_TIME))map.put(DATA_TIME,object.get(DATA_TIME).toString());
            if (object.containsKey(DRIVER_NO))map.put(DRIVER_NO,object.get(DRIVER_NO).toString());
            if (object.containsKey(LON))map.put(LON,object.get(LON).toString());
            if (object.containsKey(LAT))map.put(LAT,object.get(LAT).toString());
            if (object.containsKey(CITY_CODE))map.put(CITY_CODE,object.get(CITY_CODE).toString());
        }catch (Exception e){
            e.printStackTrace();
        }
        return map;
    }

    /**
     * 通过经纬度获取距离(单位：米)
     */
    public static double getDistance(double lng1, double lat1, double lng2,
                                     double lat2) {
        lng1 *= 0.01745329251994329D;
        lat1 *= 0.01745329251994329D;
        lng2 *= 0.01745329251994329D;
        lat2 *= 0.01745329251994329D;
        double var12 = Math.sin(lng1);
        double var14 = Math.sin(lat1);
        double var16 = Math.cos(lng1);
        double var18 = Math.cos(lat1);
        double var20 = Math.sin(lng2);
        double var22 = Math.sin(lat2);
        double var24 = Math.cos(lng2);
        double var26 = Math.cos(lat2);
        double[] var28 = new double[3];
        double[] var29 = new double[3];
        var28[0] = var18 * var16;
        var28[1] = var18 * var12;
        var28[2] = var14;
        var29[0] = var26 * var24;
        var29[1] = var26 * var20;
        var29[2] = var22;
        double var30 = Math.sqrt((var28[0] - var29[0]) * (var28[0] - var29[0])
                + (var28[1] - var29[1]) * (var28[1] - var29[1])
                + (var28[2] - var29[2]) * (var28[2] - var29[2]));
        return Math.asin(var30 / 2.0D) * 1.27420015798544E7D;
    }
}
